package utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String formatDate(String pattern){
        return formatDate(pattern,new Date());
    }

    public static String formatDate(String pattern, Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static Date parseData(String pattern,String dateStr){
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            return dateFormat.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date getBeforeTime(int i) throws ParseException {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.add(Calendar.DATE, -i);
        String dateStr = sdf.format(cal.getTime());
        return sdf.parse(dateStr);
    }

    public static Date getAfterTime(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, i);
        return cal.getTime();
    }
}
